package com.application.magazine.bookitem;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.application.magazine.Magazine;
import com.application.magazine.R;


public class BooksAdapter extends BaseAdapter {
	
	private Context context;
	private final List<Magazine> magazines;
	public Activity magazineBookSportMainActivity;
	public BooksAdapter(Context context, List<Magazine> magazines, Activity a) {
		this.context = context;
		this.magazines = magazines;
		this.magazineBookSportMainActivity = a;
	} 
	
	@Override
	public int getCount() {
		return magazines.size();
	}

	@Override
	public View getView(final int position, final View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	 
		View gridView;
	 
		if (convertView == null) {
			
			gridView = new View(context);
	 
			// get layout from mobile.xml
			gridView = inflater.inflate(R.layout.book_item_layout, null);
	  
			final Magazine mag = magazines.get(position);
			boolean status = false;
			if (mag != null) {
				if("Y".equals(mag.status)){
					status = true;
				}
				TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
				textView.setText(mag.title);

				try {
					String path = mag.tumbnail;
					File imgFile = new File(path);
					if(imgFile.isFile()){
					Bitmap icon = BitmapFactory.decodeFile(path);
					
					ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_cover);
					imageView.setImageBitmap(icon);
					}

				}catch (Exception e) {
					// TODO: handle exception
				} 
				ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_new);
				imageView.setVisibility(status ? View.VISIBLE : View.INVISIBLE);
				
				
				//Manage clickable book
				FrameLayout bookclick = (FrameLayout) gridView.findViewById(R.id.BookClick);
				bookclick.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
//						Intent i = new Intent(magazineBookSportMainActivity, MagazineBookRead.class);
//						SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
//						  SharedPreferences.Editor editor = preferences.edit();
//						  editor.putInt("mag_id",mag.id);
//						  editor.putBoolean("bookmark",false);
//						  editor.putInt("page", 0);
//						  editor.commit();
//						magazineBookSportMainActivity.startActivity(i);
//						magazineBookSportMainActivity.finish();
						
						File file = new File(mag.path);
						Intent intent = new Intent(Intent.ACTION_VIEW);
						intent.setDataAndType(Uri.fromFile(file), "application/pdf");
						intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
						magazineBookSportMainActivity.startActivity(intent);
						
					}	
				});
				bookclick.setOnLongClickListener(new OnLongClickListener() {
					
					@Override
					public boolean onLongClick(View v) {
						// TODO Auto-generated method stub
						showOptionsMenu(position);
						return false;
					}
				});
				
				
			} else {
				//TODO error
			}
	 	 
		} else {
			gridView = (View) convertView;
		}
		
		return gridView;
	}

	public void showOptionsMenu(final int position)
	{
		String[] delete = {"Delete"};
		
	new AlertDialog.Builder(magazineBookSportMainActivity)
	.setTitle("").setCancelable(true).setItems(delete,
	          new DialogInterface.OnClickListener() {
	               public void onClick(DialogInterface dialoginterface, int i) {
	                   //take actions here according to what the user has selected
	            	   Toast.makeText(magazineBookSportMainActivity, magazineBookSportMainActivity.getResources().getString(R.string.deleteBookDownload)+" "+position, Toast.LENGTH_SHORT).show();
	            	   
	               }
	         }
	)
	.show();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
}
